package com.uzai.console.service.tb.tbexclusiveolan.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.remoteapi.CheakUsersRelationIdDto;
import com.uzai.console.dto.remoteapi.DirectionalGoodsQuery;
import com.uzai.console.dto.tb.tbexclusiveplan.TbExclusivePlanDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.remoteapi.TlbRemoteApiService;
import com.uzai.console.service.tb.tbexclusiveolan.TbExclusivePlanService;
import com.uzai.console.vo.remoteapi.cps.DirectionalGoods;
import com.uzai.console.vo.remoteapi.cps.TlbDirectionalGoodsVo;
import com.uzai.console.vo.tb.tbexclusiveplan.TbExclusivePlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
@Slf4j
public class TbExclusivePlanServiceImpl implements TbExclusivePlanService {

    @Autowired
    private TlbRemoteApiService tlbRemoteApiService;
    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分页查询推老板淘宝定向商品列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return TbExclusiveGoodsVo
     */
    public Page<DirectionalGoods> findTlbTbExclusiveGoodsList(DirectionalGoodsQuery directionalGoodsQuery){
        //返回到页面分页对象
        Page<DirectionalGoods> pageVo = new Page<>();
        directionalGoodsQuery.convert(pageVo);
        TlbDirectionalGoodsVo tlbDirectionalGoodsVo = tlbRemoteApiService.queryTbDirectionalGoods(directionalGoodsQuery);
        if(tlbDirectionalGoodsVo != null){
            pageVo.setRecords(tlbDirectionalGoodsVo.getRecords());
            pageVo.setTotal(tlbDirectionalGoodsVo.getTotal());
        }
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询淘宝定向计划详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public TbExclusivePlanVo findTbExclusivePlan(Long merId) {

        TbExclusivePlanVo tbExclusivePlanVo = new TbExclusivePlanVo();

        //查询运营商淘宝定向计划信息
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.TB_EXCLUSIVE_PLAN.getValue());
        if (merBaseConfig != null) {
            tbExclusivePlanVo = JSONObject.parseObject(merBaseConfig.getVal(), TbExclusivePlanVo.class);
        }
        return tbExclusivePlanVo;
    }

    /**
     * 修改淘宝定向计划详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbExclusivePlanDto
     * @return null
     */
    @Transactional
    public void updateTbExclusivePlan(TbExclusivePlanDto tbExclusivePlanDto){

        if(tbExclusivePlanDto.getAppId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入推老板appID");
        }

        if(tbExclusivePlanDto.getCommissionType() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择定向计划类型");
        }

        if(tbExclusivePlanDto.getCommissionType() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择佣金使用规则");
        }

        //验证此appId是否在推老板平台绑定渠道id
        CheakUsersRelationIdDto cheakUsersRelationIdDto = new CheakUsersRelationIdDto();
        cheakUsersRelationIdDto.setMemberNo(tbExclusivePlanDto.getAppId());
        boolean result = tlbRemoteApiService.cheakUsersRelationId(cheakUsersRelationIdDto);
        if(!result){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "当前推老板账号未申请共享联盟对应的渠道id，请至[推老板后台—淘宝联盟-定向商品]顶部申请渠道id");
        }

        TbExclusivePlanVo tbExclusivePlanVo = new TbExclusivePlanVo();
        BeanUtils.copyProperties(tbExclusivePlanDto, tbExclusivePlanVo);

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(tbExclusivePlanDto.getMerId(), MerConfigKeyEnum.TB_EXCLUSIVE_PLAN.getValue());

        Integer now = DateUtil.getNowTime();
        //修改
        if (merBaseConfig != null){
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(tbExclusivePlanVo)));
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        } else { //增加
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(tbExclusivePlanDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.TB_EXCLUSIVE_PLAN.getValue());
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(tbExclusivePlanVo)));
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }

        //保存到Redis当中
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.TB_EXCLUSIVE_PLAN_CONFIG.getKey(), tbExclusivePlanDto.getMerId()), merBaseConfig.getVal());
    }



}
